package com.bnlr.lrparking.util;

/**
 * Created by hp on 2017/8/15.
 */

public class PermissionsDispatcher {


    /*private static final int REQUEST_ONNEED = 1;

    private static final String[] PERMISSION_ONNEED = new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"};

    private PermissionsDispatcher() {
    }

    public static void onNeedWithCheck(PrakFragment target) {
        if (PermissionUtils.hasSelfPermissions(target.getActivity(), PERMISSION_ONNEED)) {
            target.onNeed();
        } else {
            if (PermissionUtils.shouldShowRequestPermissionRationale(target.getActivity(), PERMISSION_ONNEED)) {
                target.onRationale(new OnNeedPermissionRequest(target));
            } else {
                requestPermissions(target.getActivity(), PERMISSION_ONNEED, REQUEST_ONNEED);
            }
        }
    }

    public static void onRequestPermissionsResult(PrakFragment target, int requestCode, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_ONNEED:
                if (PermissionUtils.getTargetSdkVersion(target.getActivity()) < 23 && !PermissionUtils.hasSelfPermissions(target.getActivity(), PERMISSION_ONNEED)) {
                    target.onDenied();
                    return;
                }
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.onNeed();
                } else {
                    target.onDenied();
                }
                break;
            default:
                break;
        }
    }

    private static final class OnNeedPermissionRequest implements PermissionRequest {
        private final WeakReference<PrakFragment> weakTarget;

        private OnNeedPermissionRequest(PrakFragment target) {
            this.weakTarget = new WeakReference<>(target);
        }

        @Override
        public void proceed() {
            PrakFragment target = weakTarget.get();
            if (target == null) return;
            requestPermissions(target.getActivity(), PERMISSION_ONNEED, REQUEST_ONNEED);
        }

        @Override
        public void cancel() {
            PrakFragment target = weakTarget.get();
            if (target == null) return;
            target.onDenied();
        }
    }*/
}
